package org.etlt.extract;

import org.etlt.job.JobContext;
import org.etlt.job.PkRange;

import java.util.List;

public abstract class Extractor implements AutoCloseable{
    private String name;
    protected boolean closed = false;
    protected int skip = 0;

    protected int index = 0;

    protected PkRange pkFullRange;

    public abstract ExtractorSetting getSetting();
    /**
     * extract data and set it to context
     * @param context
     */
    public abstract void extract(JobContext context);

    public abstract List<String> getColumns();

    public abstract <T extends  Extractor> T createInstance();


    public <T extends  Extractor> T createInstance(PkRange pkRange){
        return createInstance();
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() +
                "{" + getName() +  "}";
    }

    protected void close(AutoCloseable... resources) {
        for (AutoCloseable resource : resources) {
            try {
                if (resource != null)
                    resource.close();
            } catch (Exception e) {
            }
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void init(JobContext context){}

    public abstract boolean isClosed();

    public PkRange getPkFullRange() {
        return pkFullRange;
    }

    protected void setPkFullRange(PkRange pkFullRange) {
        this.pkFullRange = pkFullRange;
    }

}
